package cn.tedu.service.impl;

import cn.tedu.mapper.ProductMapper;
import cn.tedu.mapper.TypesMapper;
import cn.tedu.pojo.Product;
import cn.tedu.pojo.ProductExample;
import cn.tedu.pojo.Types;
import cn.tedu.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    TypesMapper typesMapper;

    @Override
    public List<Product> selectProductByMH(String name, Integer isHot, Integer typeId) {
        ProductExample example  = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        if(name!=null){
            criteria.andNameLike("%"+name+"%");//模糊查询的name
        }
        if(isHot!=2){
            criteria.andIsHotEqualTo(isHot);//是否热卖的条件查询
        }
        if(typeId!=-1){
            criteria.andTypeidEqualTo(typeId);  //品牌条件查询
        }
        List<Product> products = productMapper.selectByExample(example);
        //遍历products集合数据过程中存入 types数据
        for (Product product : products) {
            Types types = typesMapper.selectByPrimaryKey(product.getTypeid());
            product.setTypes(types);
        }
        return products;
    }

    @Override
    public List<Product> selectProductByPage(String name, Integer isHot, Integer typeId, Integer pageNo, Integer pageSize) {
        Map map = new HashMap<>();
        map.put("name",name);
        map.put("isHot",isHot);
        map.put("typeId",typeId);

        map.put("start",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);

        List<Product> products = productMapper.selectByLimit(map);
        //遍历products集合数据过程中存入 types数据
        for (Product product : products) {
            Types types = typesMapper.selectByPrimaryKey(product.getTypeid());
            product.setTypes(types);
        }
        return products;
    }

    @Override
    public void deleteById(int id) {
        productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void addProduct(Product product) {
        productMapper.insert(product);
    }

    @Override
    public Product selectProductById(Integer id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateProduct(Product product) {
        productMapper.updateByPrimaryKey(product);
    }


}
